package com.icss.clothes.rabbitmq;

import com.icss.clothes.entity.Order_detail;
import com.icss.clothes.entity.Orders;
import com.icss.clothes.entity.Shopdetail;
import com.icss.clothes.service.imp.OrderService;
import com.icss.clothes.service.imp.ShopDetailService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 购票RabbitMQ的消费者
 */
@Component
public class ShopConsumer {
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private OrderService orderService;
    //注册监听器并指定消息队列名称
    @RabbitListener(queues = "ticket-queue")
    public void disposeShopQueue(MQMessage mqMessage){
        //当监听到ticket-queue消息队列中有消息实，就会被执行
        //要处理购物的流程,也就是让用户实现真正的购买
        //需要有一个能够实现生成订单的业务逻辑
        //需要一个能够得到商品详情编号的所有商品信息
        Shopdetail shopdetail=shopDetailService.findAllByCd_id(mqMessage.getCd_id());
        Orders orders=new Orders();
        orders.setOrder_id(mqMessage.getOrder_id());
        orders.setUser_id(mqMessage.getUser_id());
        orders.setOrder_price(mqMessage.getOd_number()*shopdetail.getC_price());
        orders.setOrder_state(0);
        Order_detail orderDetail=new Order_detail();
        orderDetail.setOrder_id(mqMessage.getOrder_id());
        orderDetail.setC_id(mqMessage.getC_id());
        orderDetail.setCd_id(mqMessage.getCd_id());
        orderDetail.setOd_number(mqMessage.getOd_number());
        orderService.generateOrder(orders,orderDetail);

    }
}
